package com.meals.system.service.impl;

import com.meals.system.api.system.vo.WhitesMealSearchGoodsVO;
import com.meals.system.common.ServiceResultEnum;
import com.meals.system.dao.WhitesMealGoodsMapper;
import com.meals.system.entity.WhitesMealGoods;
import com.meals.system.service.WhitesMealSystemGoodsService;
import com.meals.system.util.BeanUtil;
import com.meals.system.util.PageQueryUtil;
import com.meals.system.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/17
 */
@Service
public class WhitesMealSystemGoodsServiceImpl implements WhitesMealSystemGoodsService {

    @Autowired
    private WhitesMealGoodsMapper goodsMapper;

    @Override
    public PageResult getWhitsMealGoodsPage(PageQueryUtil pageUtil) {
        List<WhitesMealGoods> goodsList = goodsMapper.findWhitesMealGoodsList(pageUtil);
        int total = goodsMapper.getTotalWhitesMealGoods(pageUtil);
        PageResult pageResult = new PageResult(goodsList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public String saveWhitesMealGoods(WhitesMealGoods goods) {
        if (goodsMapper.selectBySellerIdAndName(goods.getGoodsName(),goods.getSellerId())!=null){
            return ServiceResultEnum.SAME_GOODS_EXIST.getResult();
        }
        if (goodsMapper.insertSelective(goods) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public void batchSaveWhitesMealGoods(List<WhitesMealGoods> whitesMealGoodsList) {
        if (!CollectionUtils.isEmpty(whitesMealGoodsList)){
            goodsMapper.batchInsert(whitesMealGoodsList);
        }
    }

    @Override
    public String updateWhitesMealGoods(WhitesMealGoods goods) {
        WhitesMealGoods whitesMealGoods = goodsMapper.selectByPrimaryKey(goods.getGoodsId());
        if (whitesMealGoods == null){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        WhitesMealGoods whitesMealGoods1 = goodsMapper.selectBySellerIdAndName(goods.getGoodsName(),goods.getSellerId());
        if (whitesMealGoods1 != null && whitesMealGoods1.getGoodsId().equals(whitesMealGoods.getGoodsId())){
            // 改成与数据相同名字且商家id相同的，不能修改
            return ServiceResultEnum.SAME_GOODS_EXIST.getResult();
        }
        goods.setUpdateTime(new Date());
        if (goodsMapper.updateByPrimaryKeySelective(goods) > 0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();

    }

    @Override
    public Boolean batchUpdateSellStatus(Long[] ids, int sellStatus) {
        return goodsMapper.batchUpdateSellStatus(ids, sellStatus) > 0;
    }

    @Override
    public WhitesMealGoods getWhitesMealGoodsById(Long id) {

        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult searchWhitesMealGoods(PageQueryUtil pageUtil) {
        // 查询到的结果
        List<WhitesMealGoods> goodsList = goodsMapper.findWhitesMealGoodsListBySearch(pageUtil);
        int total = goodsMapper.getTotalWhitesMealGoodsBySearch(pageUtil);
        List<WhitesMealSearchGoodsVO> whitesMealSearchGoodsVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(goodsList)){
            whitesMealSearchGoodsVOS = BeanUtil.copyList(goodsList, WhitesMealSearchGoodsVO.class);
            for (WhitesMealSearchGoodsVO whitesMealSearchGoodsVO : whitesMealSearchGoodsVOS){
                String goodsName = whitesMealSearchGoodsVO.getGoodsName();
                String goodsIntro = whitesMealSearchGoodsVO.getGoodsIntro();
                // 字符串过长导致文字超出的问题
                if (goodsName.length() > 28){
                    goodsName = goodsName.substring(0,28) + "...";
                    whitesMealSearchGoodsVO.setGoodsName(goodsName);
                }
                if (goodsIntro.length() > 30){
                    goodsIntro = goodsIntro.substring(0,30) + "...";
                    whitesMealSearchGoodsVO.setGoodsIntro(goodsIntro);
                }
            }
        }
        PageResult pageResult = new PageResult(whitesMealSearchGoodsVOS, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

}
